//
//  AWHomeViewModel.swift
//  WeiBo
//
//  Created by AaronWei on 2017/6/8.
//  Copyright © 2017年 AaronWei. All rights reserved.
//

import UIKit
import SDWebImage

class AWHomeViewModel: NSObject {
    
    lazy var statusesArray: [AWStatusesViewModel] = [AWStatusesViewModel]()
    
    func loadData(isUp: Bool, callBack: @escaping (Bool, String)->()) {
        
        let token = AWUserAccountViewModel.sharedViewModel.userAccount?.access_token
        
        var maxId: Int64 = 0
        
        var sinceId: Int64 = 0
        
        if isUp {
            
            maxId = statusesArray.last?.status?.id ?? 0
            
            if maxId > 0 {
                
                maxId -= 1
                
            }
            
        } else {
            
            sinceId = statusesArray.first?.status?.id ?? 0
            
        }
        
        var message = "没有加载到最新的微博数据"
        
        AWNetworkTools.sharedTools.requestStatuses(token: token!, maxId: maxId, sinceId: sinceId) { (responseObject, error) in
            
            if error != nil {
                
                print(error!)
                
                callBack(false, message)
                
                return
                
            }
            
            let dict = responseObject as! [String: Any]
            
            let statusesDicArray = dict["statuses"] as! [[String: Any]]
            
            //            var tempArray = [AWStatusesModel]()
            //
            //            for statusesDic in statusesDicArray {
            //
            //                let model = AWStatusesModel(dict: statusesDic)
            //
            //                tempArray.append(model)
            //
            //            }
            
            let modelArray = NSArray.yy_modelArray(with: AWStatusesModel.self, json: statusesDicArray) as! [AWStatusesModel]
            
            var tempArray = [AWStatusesViewModel]()
            
            let group = DispatchGroup()
            
            for statusesModel in modelArray {
                
                let statusesViewModel = AWStatusesViewModel()
                
                statusesViewModel.status = statusesModel
                
                tempArray.append(statusesViewModel)
                
                if statusesModel.pic_urls?.count == 1 {
                    
                    let imageUrl = statusesModel.pic_urls!.first!.thumbnail_pic!
                    
                    group.enter()
                    
                    SDWebImageManager.shared().loadImage(with: URL(string: imageUrl), options: [], progress: nil, completed: { (image, imageData, error, _, _, url) in
                        
                        group.leave()
                        
                    })
                    
                }
                
                if statusesModel.retweeted_status?.pic_urls?.count == 1 {
                    
                    let imageUrl = statusesModel.retweeted_status!.pic_urls!.first!.thumbnail_pic!
                    
                    group.enter()
                    
                    SDWebImageManager.shared().loadImage(with: URL(string: imageUrl), options: [], progress: nil, completed: { (image, imageData, error, _, _, url) in
                        
                        group.leave()
                        
                    })
                    
                }
                
            }
                        
            if isUp {
                
                self.statusesArray.append(contentsOf: tempArray)
                
            } else {
                
                self.statusesArray.insert(contentsOf: tempArray, at: 0)
                
            }
            
            if tempArray.count > 0 {
                
                message = "加载了\(tempArray.count)条微博数据"
                
            }
            
            group.notify(queue: DispatchQueue.main, execute: {
                
                callBack(true, message)
                
            })
            
            
        }
        
    }


}
